package com.centaline.crawler.test;

import com.centaline.crawler.json.Comarea;
import com.centaline.crawler.json.Data;
import com.centaline.crawler.json.JsonRootBean;
import com.centaline.crawler.pojo.City;
import com.centaline.crawler.service.CityService;
import com.centaline.crawler.service.ComareaService;
import com.centaline.crawler.util.HttpUtils;
import com.centaline.crawler.util.JsonUtil;
import com.centaline.crawler.util.JsonUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.util.Iterator;
import java.util.List;

/**
 * @author: wbzhangzp
 * @date: 2021-05-26 17:02
 */
@SpringBootTest
public class CityCrawler {

    @Autowired
    private CityService cityService;

    @Autowired
    private HttpUtils httpUtils;

    @Autowired
    private ComareaService comareaService;


    // 保存区域版块
    @Test
    public void testArea() throws Exception {
        Document doc = Jsoup.parse(new File("E:\\crawler\\a.html"), "utf8");
        Elements elements = doc.select("#ri010 > div > ul > li > ul");

        Elements cityEle = elements.select(".on");
        System.out.println("获取class为on的值：" + cityEle.text());
        String area = cityEle.text();

        System.out.println("=====================================================================");
        Elements sections = elements.get(1).select("ul > li");

        System.out.println("=======================================================================");
        for (int i = 0; i < sections.size(); i++) {
            City city = new City();
            city.setArea(area);

            // System.out.println("第" + i +  "的版块是：" + sections.get(i).text());
            // 当不为a.html时，即 当area!=邗江时，使用下面的section进行保存
//            String section = sections.get(i).text();
//            city.setSection(section);
//            this.cityService.save(city);

            // 当area=邗江时，使用下面的section
            Elements aH = sections.get(i).select("a");
            String section = aH.text();
            city.setSection(section);
            // this.cityService.save(city);
            // System.out.println("hello");
        }
        System.out.println("保存数据成功");
        System.out.println("========================================================================");
        // System.out.println(elements);

    }


    // 保存地图经纬度
    @Test
    public void testMap() throws Exception {

        // String html = httpUtils.doGetHtml("");

        // 邗江
        // String html = httpUtils.doGetHtml("https://map.fang.com/ajaxSearch.html?city=yz&type=esf&xfPrice=&esfPrice=&zfPrice=&room=&area=&purpose=0&keyword=&zoom=14&newcodeType=&newcode=&pagingType=&pagingNum=1&x1=119.28553949739756&x2=119.52355452532132&y1=32.368538814306646&y2=32.399030309229964&subwayline=&subwaystation=&issamllcity=0&noevent=true");

        // 广陵
        // String html = httpUtils.doGetHtml("https://map.fang.com/ajaxSearch.html?city=yz&type=esf&xfPrice=&esfPrice=&zfPrice=&room=&area=&purpose=0&keyword=&zoom=14&newcodeType=&newcode=&pagingType=&pagingNum=1&x1=119.31945951345433&x2=119.55747454137808&y1=32.38177339933461&y2=32.419575713723&subwayline=&subwaystation=&issamllcity=0&noevent=true");

        // 江都
        // String html = httpUtils.doGetHtml("https://map.fang.com/ajaxSearch.html?city=yz&type=esf&xfPrice=&esfPrice=&zfPrice=&room=&area=&purpose=0&keyword=&zoom=14&newcodeType=&newcode=&pagingType=&pagingNum=1&x1=119.45736737534615&x2=119.6953824032699&y1=32.42128253780545&y2=32.4590681436305&subwayline=&subwaystation=&issamllcity=0&noevent=true");

        // 仪征
        // String html = httpUtils.doGetHtml("https://map.fang.com/ajaxSearch.html?city=yz&type=esf&xfPrice=&esfPrice=&zfPrice=&room=&area=&purpose=0&keyword=&zoom=14&newcodeType=&newcode=&pagingType=&pagingNum=1&x1=119.0721739726676&x2=119.31018900059136&y1=32.25923306393545&y2=32.297087087016465&subwayline=&subwaystation=&issamllcity=0&noevent=true");

        String html = httpUtils.doGetHtml("https://map.fang.com/ajaxSearch.html?city=yz&type=esf&xfPrice=&esfPrice=&zfPrice=&room=&area=&purpose=0&keyword=&zoom=14&newcodeType=&newcode=&pagingType=&pagingNum=1&x1=119.34594155988849&x2=119.58395658781224&y1=32.76827550135283&y2=32.80591359370283&subwayline=&subwaystation=&issamllcity=0&noevent=true");

        Document doc = Jsoup.parse(html);
        Elements body = doc.select("body");
        String json = body.text();
        System.out.println(json);

        JsonRootBean jsonRootBean = JsonUtils.fromJson(json, JsonRootBean.class);
        Data data = jsonRootBean.getData();
        List<Comarea> comareaList = data.getComarea();
        Iterator<Comarea> iterator = comareaList.iterator();
        while (iterator.hasNext()) {
            Comarea comarea = iterator.next();
            List<Comarea> list = this.comareaService.findByXAndY(comarea.getX(), comarea.getY());
            if (list.size() > 0) {
                System.out.println("已保存了相同地点的经纬度，终止本次循环执行下一次循环");
                continue;
            }
            System.out.println("该版块为：" + comarea.getComarea());
            System.out.println("经度为：" + comarea.getX());
            System.out.println("纬度为：" + comarea.getY());
            System.out.println("==================================================");
            this.comareaService.saveArea(comarea);
        }

        System.out.println("保存数据成功");
        System.out.println("========================================================================");

    }





}
